package cn.tedu._07springaop.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect
@Component
public class LogAspect {

    @Pointcut("execution(public int cn.tedu._07springaop.aop.CalculatorImpl.*(..))")
    public void doTime() {
    }
//
//    @Before("doTime()")
//    public void beforeMethod(JoinPoint joinPoint) {
//        String methodName = joinPoint.getSignature().getName();
//        String args = Arrays.toString(joinPoint.getArgs());
//        System.out.println("[前置条件]" + methodName + "方法开始了，参数为" + args);
//    }
//
//    @AfterReturning(value = "doTime()", returning = "result")
//    public void afterReturning(JoinPoint joinPoint, Object result) {
//        String methodName = joinPoint.getSignature().getName();
//        System.out.println("[返回通知]" + methodName + "方法正常返回了，结果为" + result);
//    }
//
//    @AfterThrowing(value = "doTime()", throwing = "ex")
//    public void afterThrowing(JoinPoint joinPoint, Throwable ex) {
//        String methodName = joinPoint.getSignature().getName();
//        System.out.println("[异常通知]" + methodName + "方法异常了，异常为" + ex.getMessage());
//    }
//
//    @After("doTime()")
//    public void afterMethod(JoinPoint joinPoint) {
//        String methodName = joinPoint.getSignature().getName();
//        System.out.println("[后置条件]" + methodName + "方法结束了");
//    }


    @Around("doTime()")
    public Object aroundMethod(ProceedingJoinPoint joinPoint) {
        Object result = null;
        try {
            System.out.println("[环绕通知]:前置通知");
            result = joinPoint.proceed();
            System.out.println("[环绕通知]:返回通知");
        } catch (Throwable throwable) {
            System.out.println("[环绕通知]:异常通知");
        } finally {
            System.out.println("[环绕通知]:后置通知");
        }
        return result;
    }
}
